/* $Id: ClientRoleDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.ClientRoleDAO;
import ae.pcfc.etks.online.common.domain.TTscClientRole;
import ae.pcfc.etks.online.common.exception.LookupException;

@Repository
public class ClientRoleDAOImpl extends SingleClientBaseDAO implements ClientRoleDAO {

	/**
	 * getClientRoleByRoleId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleDAO#getClientRoleByRoleId(java.lang.String)
	 * 
	 */
	public TTscClientRole getClientRoleByRoleId(String roleId) throws LookupException {

		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscClientRole.class);
		if (roleId != null)
			criteria.add(Restrictions.eq("crId", roleId));

		criteria.add(Restrictions.eq("crStatus", ConstantsCommon.ACTIVE));
		
		@SuppressWarnings("unchecked")
		List<TTscClientRole> list = criteria.list();

		if (list.size() > 0)
			return list.get(0);
		else
			return null;
	}

	/**
	 * findClientRolesByRoleIds
	 * 
	 * @see ae.pcfc.etks.online.common.dao.ClientRoleDAO#findClientRolesByRoleIds(java.util.List)
	 * 
	 */
	public List<TTscClientRole> findClientRolesByRoleIds(List<Integer> roleIds) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(TTscClientRole.class);
		if (roleIds != null && roleIds.size() > 0){
			List<String> strRoleIds = new ArrayList<String>();
			for(Integer id : roleIds){
				strRoleIds.add(id.toString());
			}
			criteria.add(Restrictions.in("crId", strRoleIds));
		}
		
		criteria.add(Restrictions.eq("crStatus", ConstantsCommon.ACTIVE));
		@SuppressWarnings("unchecked")
		List<TTscClientRole> list = criteria.list();

		return (list != null && list.size() > 0 )? list : null;	
	}

}
